package com.example.administrator.questionanswer.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.questionanswer.R;
import com.example.administrator.questionanswer.greendao.QuestionBean;

import java.text.ParseException;
import java.util.HashSet;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/2 0002.
 */

public class QuestionAdapter extends BaseAbstactRecycleAdapter<QuestionBean, QuestionAdapter.QuestionAdapterViewHolder> {

    HashSet<String> hashSet;

    Context context;

    public QuestionAdapter(Context context) {
        this.context = context;
    }

    @Override
    public void getViewHolder(final QuestionAdapterViewHolder baseViewHolder, final int position, List<QuestionBean> mList) throws ParseException {
        if (position == 5) {
            hashSet = new HashSet<>();
        }
        final TextView[] textViews = new TextView[6];
        textViews[0] = baseViewHolder.txtQuestionA;
        textViews[1] = baseViewHolder.txtQuestionB;
        textViews[2] = baseViewHolder.txtQuestionC;
        textViews[3] = baseViewHolder.txtQuestionD;
        textViews[4] = baseViewHolder.txtQuestionE;
        textViews[5] = baseViewHolder.txtQuestionF;

        final QuestionBean questionBean = mList.get(position);
        Long quesrioId = questionBean.getQuesrioId();
        final String questionName = questionBean.getQuestionName();
        String questionA = questionBean.getQuestionA();
        String questionAContent = questionBean.getQuestionAContent();
        String questionB = questionBean.getQuestionB();
        String questionBContent = questionBean.getQuestionBContent();
        String questionC = questionBean.getQuestionC();
        String questionCContent = questionBean.getQuestionCContent();
        String questionD = questionBean.getQuestionD();
        String questionDContent = questionBean.getQuestionDContent();
        String questionE = questionBean.getQuestionE();
        String questionEContent = questionBean.getQuestionEContent();
        String questionF = questionBean.getQuestionF();
        String questionFContent = questionBean.getQuestionFContent();
        baseViewHolder.txtQuestionName.setText(quesrioId + questionName);
        if (questionAContent != null) {
            baseViewHolder.txtQuestionA.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionA.setText(questionA + "、" + questionAContent);
        }

        if (questionBContent != null) {
            baseViewHolder.txtQuestionB.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionB.setText(questionB + "、" + questionBContent);
        }

        if (questionCContent != null) {
            baseViewHolder.txtQuestionC.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionC.setText(questionC + "、" + questionCContent);
        }

        if (questionDContent != null) {
            baseViewHolder.txtQuestionD.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionD.setText(questionD + "、" + questionDContent);
        }
        if (questionEContent != null) {
            baseViewHolder.txtQuestionE.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionE.setText(questionE + "、" + questionEContent);
        }

        if (questionFContent != null) {
            baseViewHolder.txtQuestionF.setVisibility(View.VISIBLE);
            baseViewHolder.txtQuestionF.setText(questionF + "、" + questionFContent);
        }

        baseViewHolder.txtQuestionA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                changTextColor(position, 0, textViews, questionBean
                );

            }
        });
        baseViewHolder.txtQuestionB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changTextColor(position, 1, textViews, questionBean);
            }
        });
        baseViewHolder.txtQuestionC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changTextColor(position, 2, textViews, questionBean);
            }
        });
        baseViewHolder.txtQuestionD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changTextColor(position, 3, textViews, questionBean);
            }
        });
        baseViewHolder.txtQuestionE.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changTextColor(position, 4, textViews, questionBean);
            }
        });
        baseViewHolder.txtQuestionF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changTextColor(position, 5, textViews, questionBean);
            }
        });

    }

    @Override
    public QuestionAdapterViewHolder creatHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question, parent, false);
        return new QuestionAdapterViewHolder(view);
    }


    public void changTextColor(int item, int postion, TextView[] textViews, QuestionBean questionBean) {

        if (item != 5) {
            for (int i = 0; i < textViews.length; i++) {
                if (i == postion) {
                    textViews[postion].setTextColor(Color.BLUE);
                    Drawable drawable = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    textViews[i].setCompoundDrawables(drawable, null, null, null);
                    checkAswer(textViews[i].getText().toString(), questionBean);
                } else {
                    textViews[i].setTextColor(Color.BLACK);
                    Drawable drawable = context.getResources().getDrawable(R.drawable.ic_radio_button_unchecked_black_24dp);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    textViews[i].setCompoundDrawables(drawable, null, null, null);
                }
            }
        } else {
            for (int i = 0; i < textViews.length; i++) {
                if (i == postion) {
                    if (textViews[i].getCurrentTextColor() == Color.BLUE) {
                        textViews[postion].setTextColor(Color.BLACK);
                        Drawable drawable = context.getResources().getDrawable(R.drawable.ic_radio_button_unchecked_black_24dp);
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                        textViews[i].setCompoundDrawables(drawable, null, null, null);
                        String answer = textViews[postion].getText().toString();
                        if (answer.startsWith("A")) {
                            if (hashSet.contains("A")) {
                                hashSet.remove("A");
                            }
                        } else if (answer.startsWith("B")) {
                            if (hashSet.contains("B")) {
                                hashSet.remove("B");
                            }
                        } else if (answer.startsWith("C")) {
                            if (hashSet.contains("C")) {
                                hashSet.remove("C");
                            }
                        } else if (answer.startsWith("D")) {
                            if (hashSet.contains("D")) {
                                hashSet.remove("D");
                            }
                        } else if (answer.startsWith("E")) {
                            if (hashSet.contains("E")) {
                                hashSet.remove("E");
                            }
                        } else if (answer.startsWith("F")) {
                            if (hashSet.contains("F")) {
                                hashSet.remove("F");
                            }
                        }
                    } else {
                        textViews[postion].setTextColor(Color.BLUE);
                        Drawable drawable = context.getResources().getDrawable(R.drawable.ic_radio_button_checked_black_24dp);
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                        textViews[i].setCompoundDrawables(drawable, null, null, null);
                        String answer = textViews[postion].getText().toString();
                        if (answer.startsWith("A")) {
                            hashSet.add("A");
                        } else if (answer.startsWith("B")) {
                            hashSet.add("B");
                        } else if (answer.startsWith("C")) {
                            hashSet.add("C");
                        } else if (answer.startsWith("D")) {
                            hashSet.add("D");
                        } else if (answer.startsWith("E")) {
                            hashSet.add("E");
                        } else if (answer.startsWith("F")) {
                            hashSet.add("F");
                        }
                    }
                }

            }

            StringBuffer buffer = new StringBuffer();
            for (String s : hashSet) {
                buffer.append(s).append(",");
            }
            buffer.deleteCharAt(buffer.length() - 1);

            Log.d("QuestionAdapter", "buffer:" + buffer);
            questionBean.setQuestionContent(buffer.toString());
        }


    }

    public void checkAswer(String answer, QuestionBean questionBean) {
        if (answer.startsWith("A")) {
            questionBean.setQuestionContent("A");
        } else if (answer.startsWith("B")) {
            questionBean.setQuestionContent("B");
        } else if (answer.startsWith("C")) {
            questionBean.setQuestionContent("C");
        } else if (answer.startsWith("D")) {
            questionBean.setQuestionContent("D");
        } else if (answer.startsWith("E")) {
            questionBean.setQuestionContent("E");
        } else if (answer.startsWith("F")) {
            questionBean.setQuestionContent("F");
        }
    }


    static class QuestionAdapterViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_question_name)
        TextView txtQuestionName;
        @BindView(R.id.txt_question_a)
        TextView txtQuestionA;
        @BindView(R.id.txt_question_b)
        TextView txtQuestionB;
        @BindView(R.id.txt_question_c)
        TextView txtQuestionC;
        @BindView(R.id.txt_question_d)
        TextView txtQuestionD;
        @BindView(R.id.txt_question_e)
        TextView txtQuestionE;
        @BindView(R.id.txt_question_f)
        TextView txtQuestionF;


        public QuestionAdapterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface OnQuestionSelected {

        void onAswerSelect();
    }


}
